//
//  BaseController.m
//  Medicine
//
//  Created by laber on 15/5/18.
//  Copyright (c) 2015年 laber. All rights reserved.
//

#import "BaseController.h"

@interface BaseController ()

@end

@implementation BaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//更改状态栏为白
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:BAR_COLOR] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
//    UIView *bottom = [[UIView alloc] initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, 1)];
//    bottom.backgroundColor = [UIColor colorWithHexString:@"#36915d"];
//    [self.navigationController.navigationBar addSubview:bottom];
    
    //self.navigationController.navigationBar.barTintColor = BAR_COLOR;
    
}

- (instancetype)initViewController:(NSString *) _SBName identifier:(NSString *)_identifier{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:_SBName bundle:nil];
    BaseController *vc = [storyboard instantiateViewControllerWithIdentifier:_identifier];
    return vc;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/**
 * left:{
 *       type:'back|btn'
 *        icon:'只有btn才需要指定',
 *        action:'动作名'
 *        },
 *	middle:{
 *        type: 1.标题形式,2.TAB
 *        title:'标题内容，只有type=1'
 *        titleicon:'可以为空，只有type=1',R.xxx
 *        tabitems:[{text:'消息',action:'动作名'}],只有type=2
 *	},
 *	right:{
 *        type:'btn|list',
 *        icon:'图标，只有btn',
 *        text:'文本，只有btn,如指定了icon只显示icon，否则该text'
 *        items:'只有list才具有',[{icon:'图标',action:'动作',text:'文本'},...]
 *	}
 *  初始化导航拦样式
 *
 *  @param left   左边按钮的样式
 *  @param middle 中间按钮的样式
 *  @param right  右边按钮的样式
 */
-(void)createWithLeftStyle:(NSDictionary *)left middleStyle:(NSDictionary *)middle rightStyle:(NSDictionary *)right{
    
    if (left) {
        if ([left[@"type"] isEqualToString:@"back"]) {
            self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"nav_back" highlightedIcon:@"nav_back" title:@"" size:CGSizeMake(22, 25) target:self action:@selector(clickBack:)];
        }else{
            self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:left[@"icon"] highlightedIcon:left[@"icon"] target:self action:@selector(leftNactionItemClick)];
        }
    }else{
        self.navigationItem.leftBarButtonItem = nil;
    }
    
    if(middle){
        if ([middle[@"type"] isEqualToString:@"title"]) {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , FONT_BIG*6, 44)];
            titleLabel.text = middle[@"title"];
            titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明
            titleLabel.font = [UIFont boldSystemFontOfSize:FONT_BIG];  //设置文本字体与大小
            titleLabel.textColor = [UIColor whiteColor];  //设置文本颜色
            titleLabel.textAlignment = NSTextAlignmentCenter;
            self.navigationItem.titleView = titleLabel;
            //self.title = middle[@"title"];
        }else{
            
        }
    }else{
        self.navigationItem.titleView = nil;
    }
    
    if (right) {
        if ([right[@"type"] isEqualToString:@"btn"]) {
            if (right[@"icon"]) {
                self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:right[@"icon"] highlightedIcon:right[@"icon"] target:self action:@selector(rightNactionItemClick:)];
            }else{
                self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:right[@"title"] size:CGSizeMake(FONT_MIDDLE*2+5, FONT_MIDDLE) target:self action:@selector(rightNactionItemClick:)];
            }
            
        }else{
            
        }
    }else{
        self.navigationItem.rightBarButtonItem = nil;
    }
}

- (UITextField *)createTextFiled:(NSString *)leftImage plach:(NSString *)plach{
    UIView *leftBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:leftImage]];
    [leftBack addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(leftBack.mas_centerX);
        make.centerY.equalTo(leftBack.mas_centerY);
    }];
    UITextField *textFiled = [[UITextField alloc] init];
    [textFiled.layer setCornerRadius:5];
    [textFiled.layer setBorderColor:LINE_COLOR.CGColor];
    [textFiled.layer setBorderWidth:1.0f];
    textFiled.leftView = leftBack;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.placeholder = plach;
    return textFiled;
}

//左边为返回按钮时调用
-(void)clickBack:(UIButton *)btn{
    //btn.alpha = 0.5;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)leftNactionItemClick{}

-(void)rightNactionItemClick:(UIButton *)btn{}

-(void)initChildViews{}
-(void)makeConstraints{}
-(void)loadHttpData{};
@end
